package com.topscomm.mms.xxlJob;
import com.topscomm.cbo.service.impl.CboRoleService;
import com.topscomm.mms.MmsService;
import com.topscomm.mms.service.impl.MmsDeptExtraInfoService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.common.ParameterCache;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@JobHandler(value = "SyncPermissionRole")
@Component
public class SyncPermissionRole extends IJobHandler {
    @Autowired
    private CboRoleService cboRoleService;
    @Autowired
    private MmsDeptExtraInfoService mmsDeptExtraInfoService;
    /**
     * @description: // 处理需要同步的角色
     * @param: [param]
     * @return:
     * @author: hdy
     * @date: 2022/11/17 15:59
     * @modify:
    */
    @Override
    public ReturnT<String> execute(String param) {
        try {
        	String time = ParameterCache.getValue("SyncPermissionRoleTime", "");
        	StringBufferProxy sql = new StringBufferProxy();
//        	sql.appendSingle("SELECT CODE, NAME FROM CBOROLE b WHERE b.CODE LIKE 'permission%' "
//        			+ "AND b.CODE NOT LIKE 'permissionProductline%' "
//        			+ "AND b.CODE NOT LIKE 'permissionProvince%' "
//        			+ "AND ROUND(TO_NUMBER(SYSDATE - b.MODIFIEDON) * 24 * 60) < {0}",time);
//        	List<Map<String, Object>> roleList = cboRoleService.selectExecute(sql.toString());
        	sql.clear();
        	sql.appendSingle("SELECT c.CODE as usercode,b.CODE, b.NAME FROM CBOUSERROLE a,CBOROLE b, CBOUSER c WHERE a.ROLEID = b.ID "
        			+ "AND c.id = a.USERID AND  b.CODE LIKE 'permission%' "
        			+ "AND b.CODE NOT LIKE 'permissionProductline%' "
        			+ "AND b.CODE NOT LIKE 'permissionProvince%' "
        			+ "AND ROUND(TO_NUMBER(SYSDATE - a.MODIFIEDON) * 24 * 60) < {0}", time);
        	List<Map<String, Object>> userRoleList = cboRoleService.selectExecute(sql.toString());
        	List<Map<String, Object>> roleList = new ArrayList<Map<String,Object>>();
        	Map<String, Object> roletemp = new HashMap<>();
        	for(Map<String, Object> role : userRoleList) {
        		roletemp = new HashMap<>();
        		roletemp.putAll(role);
        		roletemp.remove("usercode");
        		if(roleList.indexOf(roletemp) < 0) {
        			roleList.add(roletemp);
        		}
        	}
        	if(userRoleList.size()>0) {
            	Map<String, Object> roleMap = new HashMap<String, Object>();
            	for(Map<String, Object> role : roleList) {
            		String rolecode = ConvertUtil.convertToString(role.get("code"));
            		List<Map<String, Object>> userRoleListsFilter = userRoleList.stream()
        					.filter(item -> item.get("code").toString().equals(rolecode)).collect(Collectors.toList());
            		String users = userRoleListsFilter.stream().map(s ->ConvertUtil.convertToString(s.get("usercode"))).collect(Collectors.joining(","));
            		role.put("usercode", users);
            		mmsDeptExtraInfoService.sendMessageToFds(role,"specialRoles");
            	}
        	}
            return ReturnT.SUCCESS;
        } catch (Exception e) {
            return new ReturnT(ReturnT.FAIL_CODE,e.getMessage());
        }
    }
}
